package cn.nnb.control.cy;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class checkphoto extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public checkphoto() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		
		int fid =Integer.parseInt(request.getParameter("fid"));
		String fna = new String(request.getParameter("fna").getBytes("ISO-8859-1"),"utf-8");
	   String path = new String(request.getParameter("path").getBytes("ISO-8859-1"),"utf-8");
		
	   double fpr = Double.parseDouble(request.getParameter("fpr"));
		double fdis = Double.parseDouble(request.getParameter("fdis"));
	   File file =new File(path);
		String pathfail;
		String isphoto;
		if(path.length()<7){
			pathfail="pathfail";
			request.setAttribute("pathfail", pathfail);
			
			request.setAttribute("fid",fid);
			request.setAttribute("fna", fna);
			request.setAttribute("fpr", fpr);
			request.setAttribute("fdis", fdis);
			request.getRequestDispatcher("cy/FoodEdit.jsp").forward(request, response);
			
		}
		else if(! file.exists()||!path.substring(path.length()-4,path.length()).equals(".jpg")){
			pathfail="pathfail";
			request.setAttribute("fid",fid);
			request.setAttribute("fna", fna);
			request.setAttribute("fpr", fpr);
			request.setAttribute("fdis", fdis);
			
			request.setAttribute("pathfail", pathfail);
			request.getRequestDispatcher("cy/FoodEdit.jsp").forward(request, response);
		
		}
		else{
			
			isphoto="isphoto";
			request.setAttribute("fid",fid);
			request.setAttribute("fna", fna);
			request.setAttribute("fpr", fpr);
			request.setAttribute("fdis", fdis);
			request.setAttribute("path", path);
			
			request.setAttribute("isphoto", isphoto);
			request.getRequestDispatcher("cy/FoodEdit.jsp").forward(request, response);
			
			
		}
	
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
